package com.doyens.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.doyens.domain.DelStudent;
import com.doyens.domain.Student;
import com.doyens.domain.Teacher;

import com.doyens.domain.TeacherStudent;
import com.doyens.mapper.StudentMapper;
import com.doyens.mapper.TeacherMapper;

import com.doyens.service.StudentService;
import com.doyens.service.TeacherService;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Object add(TeacherStudent teacherStudent) {
        Teacher teacher = teacherStudent.getTeacher();
        List<Student> students = teacherStudent.getStudents();
        this.save(teacher);
        for (Student student : students) {
            student.setTid(student.getTid());
        }
      return   studentService.saveBatch(students);

    }

    @Override
    public Object update1(TeacherStudent teacherStudent) {

          this.updateById(teacherStudent.getTeacher());
        LambdaQueryWrapper<Student> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Student::getTid,teacherStudent.getTeacher().getTid());
        List<Student> list = studentService.list(wrapper);
        ArrayList<Integer> integers = new ArrayList<>();
        for (Student student : list) {
            integers.add(student.getTid());
        }
        if(integers.size()>0){
            studentService.removeByIds(integers);
        }
        for (Student student : teacherStudent.getStudents()) {
            student.setTid(student.getTid());
        }
        return     studentService.saveBatch(teacherStudent.getStudents());




    }

    @Override
    public Object delStudent(DelStudent delStudent) {
        List<Integer> sid = delStudent.getSid();
        if(sid.size()>0){
            studentService.removeByIds(delStudent.getSid());
              this.removeByIds(delStudent.getTid());
        }else{
            this.removeByIds(delStudent.getTid());
        }

        return true;
    }

    @Override
    public Object delManyStudent(DelStudent delStudent) {
        List<Integer> sid = delStudent.getSid();
        if(sid.size()==0){
             this.removeByIds(delStudent.getTid());
        }
        return true;
    }

}
